#include "ejeffecteddelegate.h"
#include <QApplication>
#include <QMouseEvent>
#include <QPainter>
#include <QStyleOption>
#include <QSqlTableModel>
#include <QDebug>

EJEffectedDelegate::EJEffectedDelegate(QObject *parent)
    :QStyledItemDelegate(parent)
{

}



static QRect CheckBoxRect(const QStyleOptionViewItem &viewItemStyleOptions)/*const*/
{
    //绘制按钮所需要的参数
    QStyleOptionButton checkBoxStyleOption;
    //按照给定的风格参数 返回元素子区域
    QRect checkBoxRect = QApplication::style()->subElementRect( QStyle::SE_CheckBoxIndicator, &checkBoxStyleOption);
    //返回QCheckBox坐标
    QPoint checkBoxPoint(viewItemStyleOptions.rect.x() + viewItemStyleOptions.rect.width() / 2 - checkBoxRect.width() / 2,
                         viewItemStyleOptions.rect.y() + viewItemStyleOptions.rect.height() / 2 - checkBoxRect.height() / 2);
    //返回QCheckBox几何形状
    return QRect(checkBoxPoint, checkBoxRect.size());
}


void EJEffectedDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option,const QModelIndex& index)const
{
    bool checked = index.model()->data(index, Qt::DisplayRole).toBool();
    //qDebug("checked : %d", checked);
    QStyleOptionButton checkBoxStyleOption;
    checkBoxStyleOption.state |= QStyle::State_Enabled;
    checkBoxStyleOption.state |= checked ? QStyle::State_On : QStyle::State_Off;
    checkBoxStyleOption.rect = CheckBoxRect(option);

    QApplication::style()->drawControl(QStyle::CE_CheckBox,&checkBoxStyleOption,painter);

}

bool EJEffectedDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)
{
#if 0
    if (event->type() == QEvent::MouseButtonRelease)
    {
        QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
        if(mouseEvent->button() == Qt::LeftButton && CheckBoxRect(option).contains(mouseEvent->pos()))
        {
            qDebug("row : %d, col : %d", index.row(), index.column());

            bool checked = index.data().toBool();

            bool ret = model->setData(index, !checked);

            emit clicked(!checked, index);

            auto m = reinterpret_cast<QSqlTableModel *>(model);

            m->submit();

            return ret;

        }

    }

    return QStyledItemDelegate::editorEvent(event, model, option, index);
#endif

    if((event->type() == QEvent::MouseButtonRelease) || (event->type() == QEvent::MouseButtonDblClick))
    {
        QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
        if(mouseEvent->button() != Qt::LeftButton || !CheckBoxRect(option).contains(mouseEvent->pos()))
        {
            return true;
        }
        if(event->type() == QEvent::MouseButtonDblClick)
        {
            return true;
        }
    }
    else if(event->type() == QEvent::KeyPress)
    {
        if(static_cast<QKeyEvent*>(event)->key() != Qt::Key_Space && static_cast<QKeyEvent*>(event)->key() != Qt::Key_Select)
        {
            return true;
        }
    }
    else
    {
        return true;
    }

    qDebug("row : %d, col : %d", index.row(), index.column());

    bool checked = index.model()->data(index, Qt::DisplayRole).toBool();

    bool ret =  model->setData(index, !checked, Qt::EditRole);

    model->submit();

    emit clicked (!checked, index);

    return ret;

}
